<template>
    <div class="bg">
        <div style="width: 450px; background-color: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); padding: 40px 20px">
        <el-form ref="formRef" :model="data.form" :rules="data.rules">
            <div style="margin-bottom:40px;text-align: center;font-weight: bold;font-size: 24px">欢迎登陆</div>
            
            <!-- 添加角色选择滑块 -->
            <div style="margin-bottom: 20px; text-align: center;">
                <el-switch
                    v-model="data.isAdmin"
                    active-text="管理员"
                    inactive-text="用户"
                    style="margin-bottom: 15px;"
                />
            </div>
            
            <el-form-item prop="username">
                <el-input size="large" v-model="data.form.username" autocomplete="on" prefix-icon="User" placeholder="账号" @keyup.enter="login"/>
            </el-form-item>
            <el-form-item prop="password">
                <el-input size="large" show-password v-model="data.form.password" autocomplete="on" prefix-icon="Lock" placeholder="密码" @keyup.enter="login"/>
            </el-form-item>

            <el-button style="width:100%" size="large" type="primary" @click="login">登录</el-button>
            <div style="text-align: left">没有账号？
                <a style="margin: 118px; color: #329ff1 " href="/register">注册</a>
                <a style="text-align: right; color: #ff4136" href="/forgetPassword">忘记密码！</a>
            </div>
        </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">
import {reactive, ref} from "vue";
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import request from '../utils/request.js'; // ✅ 从正确路径导入自定义实例

const router = useRouter();

const formRef = ref();

const data = reactive({
    form: { },
    isAdmin: true, // 默认选择管理员登录
    rules: {
        username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 2, message: '最少2位', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
        ]
    }
});

const login = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 根据选择的角色调用不同的登录接口
      const loginUrl = data.isAdmin ? '/login' : '/user/login';
      
      // 如果是用户登录，需要将username映射到name字段
      const loginData = data.isAdmin ? data.form : {
        name: data.form.username,
        password: data.form.password
      };
      
      request.post(loginUrl, loginData).then(res => {
        if (res.code === '200') {
          // 存储用户信息和token
          localStorage.setItem("userInfo", JSON.stringify(res.data || {}))
          localStorage.setItem("token", res.data.token || '')
          ElMessage.success('登录成功')
          router.push('/manager/home')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

</script>

<style>
.bg{
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #baccd9;
    background-size: cover;


    border-radius:6px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
    padding: 40px 20px;
}
</style>
